/*
 * Copyright © 2017 eqxiu.com 北京中网易企秀科技有限公司  All rights reserved.
 */
package cn.hermit.util;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.zip.GZIPOutputStream;

/**
 * IOUtils utility
 *
 * @author Jack Gao (Chinese name : GAO JIANGUO, Email : linux.gjg@gmail.com)
 * @date 24 Jul, 2014
 */
public class IOUtils {

	public static void closeQuietly(Closeable is) {
		if (is != null) {
			try {
				is.close();
			} catch (IOException e) {
			}

		}
	}

	/**
	 *
	 * @param file
	 *            the absolute file path
	 * @param zip
	 *            gzip or not
	 * @return byte array of the file
	 *
	 * @throws IOException
	 */
	public static byte[] file2ByteArray(File file, boolean zip) throws IOException {
		InputStream is = null;
		byte[] result = null;
		try {
			is = new BufferedInputStream(new FileInputStream(file));
			result = inputStream2ByteArray(is, zip);
		} catch (IOException e) {
			throw e;
		} finally {
			closeQuietly(is);
		}
		return result;

	}

	public static byte[] url2ByteArray(URL url, boolean zip) throws IOException {
		InputStream is = null;
		byte[] result = null;
		try {
			is = url.openStream();
			result = inputStream2ByteArray(is, zip);
		} catch (IOException e) {
			throw e;
		} finally {
			closeQuietly(is);
		}
		return result;

	}

	public static byte[] inputStream2ByteArray(InputStream is, boolean zip) throws IOException {
		GZIPOutputStream gzip = null;
		byte[] buffer = new byte[8912];
		ByteArrayOutputStream baos = new ByteArrayOutputStream(8912);
		try {
			if (zip) {
				gzip = new GZIPOutputStream(baos);
			}
			int read = 0;
			while ((read = is.read(buffer)) != -1) {
				if (zip) {
					gzip.write(buffer, 0, read);
				} else {
					baos.write(buffer, 0, read);
				}
			}
		} catch (IOException e) {
			throw e;
		} finally {
			closeQuietly(gzip);
		}
		return baos.toByteArray();

	}

	/**
	 * Copy bytes from input stream to output stream.
	 *
	 * @param inStream
	 * 		The input stream
	 * @param outStream
	 * 		The output stream
	 * @throws IOException
	 */
	public static void copy(InputStream inStream, OutputStream outStream)
			throws IOException {
		byte[] buffer = new byte[1024];
		int len;
		while ((len = inStream.read(buffer)) != -1) {
			outStream.write(buffer, 0, len);
		}
	}

	/**
	 * Copy file from source to destination.
	 *
	 * @param src
	 * 		The source
	 * @param dest
	 * 		The destination
	 * @param replaceExisting
	 * 		If true, will replace destination with source
	 * @throws IOException
	 */
	public static void copy(File src, File dest, boolean replaceExisting)
			throws IOException {
		if (ReflectionUtils.isAllEmpty(src, dest)) {
			return;
		}
		if (!src.exists() || src.isDirectory())
			return;
		if (dest.exists()) {
			if (dest.isDirectory()) {
				return;
			} else {
				if (!replaceExisting)
					return;
			}
		} else {
			if (!dest.getParentFile().exists()) {
				dest.getParentFile().mkdirs();
			}
			dest.createNewFile();
		}
		InputStream inStream = null;
		OutputStream outStream = null;
		try {
			inStream = new FileInputStream(src);
			outStream = new FileOutputStream(dest);
			copy(inStream, outStream);
		} finally {
			if (inStream != null) {
				inStream.close();
			}
			if (outStream != null) {
				outStream.close();
			}
		}
	}

	public static void main(String[] args) throws Exception{
		BufferedInputStream is = new BufferedInputStream(new FileInputStream(new File("file:/Users/JackGao/eqxiu/ws/user-ws/server-common/examples/dubbo/example-dubbo-provider/build/distributions/tmp/example-dubbo-provider-1.2.0.jar!/BOOT-INF/lib/eqxiu-boot-dubbo-1.2.0.jar!/static/index.html")));
		System.out.println(is);
	}
}